package com.javacv.plus.platform;

import lombok.extern.slf4j.Slf4j;

/**
 * 平台检测器
 * 自动识别当前运行的操作系统和架构
 */
@Slf4j
public class PlatformDetector {

    private static final String OS_NAME = System.getProperty("os.name").toLowerCase();
    private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase();

    /**
     * 检测当前平台
     */
    public static Platform detectCurrentPlatform() {
        String os = detectOS();
        String arch = detectArch();
        Platform platform = Platform.of(os, arch);
        
        log.info("检测到当前平台: {}", platform);
        return platform;
    }

    /**
     * 检测操作系统
     */
    private static String detectOS() {
        if (OS_NAME.contains("windows")) {
            return "windows";
        } else if (OS_NAME.contains("linux")) {
            return "linux";
        } else if (OS_NAME.contains("mac") || OS_NAME.contains("darwin")) {
            return "macosx";
        } else if (OS_NAME.contains("android")) {
            return "android";
        } else if (OS_NAME.contains("ios")) {
            return "ios";
        } else {
            throw new UnsupportedOperationException("不支持的操作系统: " + OS_NAME);
        }
    }

    /**
     * 检测架构
     */
    private static String detectArch() {
        if (OS_ARCH.equals("amd64") || OS_ARCH.equals("x86_64")) {
            return "x86_64";
        } else if (OS_ARCH.equals("x86") || OS_ARCH.equals("i386")) {
            return "x86";
        } else if (OS_ARCH.equals("aarch64") || OS_ARCH.equals("arm64")) {
            return "arm64";
        } else if (OS_ARCH.startsWith("arm")) {
            return "arm";
        } else {
            throw new UnsupportedOperationException("不支持的架构: " + OS_ARCH);
        }
    }

    /**
     * 检查是否为当前平台
     */
    public static boolean isCurrentPlatform(Platform platform) {
        Platform current = detectCurrentPlatform();
        return current.equals(platform);
    }

    /**
     * 检查是否为当前平台
     */
    public static boolean isCurrentPlatform(String platformString) {
        try {
            Platform platform = Platform.parse(platformString);
            return isCurrentPlatform(platform);
        } catch (Exception e) {
            log.warn("无法解析平台字符串: {}", platformString, e);
            return false;
        }
    }

    /**
     * 获取系统信息
     */
    public static SystemInfo getSystemInfo() {
        return SystemInfo.builder()
                .osName(System.getProperty("os.name"))
                .osVersion(System.getProperty("os.version"))
                .osArch(System.getProperty("os.arch"))
                .javaVersion(System.getProperty("java.version"))
                .javaVendor(System.getProperty("java.vendor"))
                .availableProcessors(Runtime.getRuntime().availableProcessors())
                .maxMemory(Runtime.getRuntime().maxMemory())
                .totalMemory(Runtime.getRuntime().totalMemory())
                .freeMemory(Runtime.getRuntime().freeMemory())
                .platform(detectCurrentPlatform())
                .build();
    }
} 